#pragma once

#include <assert.h>
#include <iostream>
//#include<cstring>

namespace dr
{
	template<class T>
	class vector
	{
	public:
		typedef T* iterator;
		typedef const T* const_iterator;

		vector()
		{
			_start = nullptr;
			_finish = nullptr;
			_end_of_storage = nullptr;
		}

		vector(size_t n, const T& v = T())
		{
			reserve(n);
			iterator it = _start;
			while (n--)
			{
				*it = v;
				++it;
				++_finish;
			}
		}

		vector(initializer_list<T> il)
		{
			reserve(il.size());
			for (auto& e : il)
			{
				push_back(e);
			}

		}

		vector(const vector<T>& v)
		{
			reserve(v.capacity());
			for (size_t i = 0; i < v.size(); i++)
			{
				push_back(v[i]);
			}

		}

		~vector()
		{
			if (_start != nullptr)
			{
				delete[] _start;
				_start = _finish = _end_of_storage = nullptr;
			}
		}

		void swap(vector<T>& x)
		{
			std::swap(_start, x._start);
			std::swap(_finish, x._finish);
			std::swap(_end_of_storage, x._end_of_storage);
		}

		vector<T>& operator= (const vector<T>& x)
		{
			vector<T> v = x;
			swap(v);

			return *this;
		}

		void reserve(size_t n)
		{
			size_t old_size = size();
			if (n > capacity())
			{
				iterator tmp = new T[n];
				if (_start != nullptr)
				{
					memcpy(tmp, _start, sizeof(T) * old_size);
					delete[] _start;
				}

				_start = tmp;
				_finish = _start + old_size;
				_end_of_storage = _start + n;
			}
		}

		void push_back(const T& v)
		{
			if (_finish == _end_of_storage)
			{
				size_t newcapacity = capacity() == 0 ? 4 : 2 * capacity();
				reserve(newcapacity);
			}
			*(_finish) = v;
			_finish++;
		}

		void pop_back()
		{
			--_finish;
		}

		iterator insert(iterator pos, const T& v)
		{
			if (_finish == _end_of_storage)
			{
				size_t len = pos - _start;
				size_t newcapacity = capacity() == 0 ? 4 : 2 * capacity();
				reserve(newcapacity);
				pos = _start + len;
			}
			iterator it = end();
			while (it >= pos)
			{
				*it = *(it - 1);
				it--;
			}
			*pos = v;
			++_finish;

			return pos;
		}

		iterator erase(iterator pos)
		{
			iterator it = pos + 1;
			while (it != _finish)
			{
				*(it - 1) = *it;
				++it;
			}
			--_finish;
			return pos;
		}

		//template <class InputIterator>
		//void insert(iterator position, InputIterator first, InputIterator last)
		//{

		//}



		size_t capacity() const
		{
			return _end_of_storage - _start;
		}

		size_t size() const
		{
			return _finish - _start;
		}

		iterator begin()
		{
			return _start;
		}

		iterator end()
		{
			return _finish;
		}

		const_iterator begin() const
		{
			return _start;
		}

		const_iterator end() const
		{
			return _finish;
		}

		T& operator[] (size_t n)
		{
			return *(_start + n);
		}

		const T& operator[](size_t n) const
		{
			return *(_start + n);
		}
		                                          
	private:
		iterator _start;
		iterator _finish;
		iterator _end_of_storage;
	};
}

//namespace dr
//{
//	template<class T>
//	class vector
//	{
//	public:
//		typedef T* iterator;
//
//		vector()
//		{
//			_start = nullptr;
//			_finish = nullptr;
//			_end_of_storage = nullptr;
//		}
//
//		void reserve(size_t n);
//			//{
//			//	if (n > capacity())
//			//	{
//			//		size_t oldsize = size();
//			//		iterator newcapacity = new T[n];
//			//		memcpy(_start, newcapacity, n * sizeof(T));
//			//		delete[] _start;
//			//		_start = newcapacity;
//			//		_finish = _start + oldsize;
//			//		_end_of_storage = _start + n;
//			//	}
//			//}
//
//			void push_back(const T& val);
//		//{
//		//	if (_start == _finish)
//		//	{
//		//		size_t newcapacity = cPcity() == 0 ? 4 : 2 * capacity();
//		//		reserve(newcapacity);
//		//	}
//		//}
//
//		size_t size() const
//		{
//			return _finish - _start;
//		}
//
//		size_t capacity() const
//		{
//			return _end_of_storage - _start;
//		}
//
//	private:
//		iterator _start;
//		iterator _finish;
//		iterator _end_of_storage;
//	};
//}
//
//template<class t>
//void dr::vector<t>::reserve(size_t n)
//{
//	if (n > capacity())
//	{
//		size_t oldsize = size();
//		iterator newcapacity = new t[n];
//		memcpy(_start, newcapacity, n * sizeof(t));
//		delete[] _start;
//		_start = newcapacity;
//		_finish = _start + oldsize;
//		_end_of_storage = _start + n;
//	}
//}
//
//template<class t>
//void dr::vector<t>::push_back(const t& val)
//{
//	if (_start == _finish)
//	{
//		size_t newcapacity = capacity() == 0 ? 4 : 2 * capacity();
//		reserve(newcapacity);
//	}
//}